﻿var selectHeaderMenuId = "";
var selectToolbarId = "";

var headerMenuArray = [ {
	name : "设备监控",
	id : "1",
	children : [ {
		name : "时间服务器查询",
		id : "11",
		src : "modules/timeServer/timeServer.html"
	}, {
		name : "被授时客户端查询",
		id : "12",
		src : "modules/timeClient/timeClient.html"
	}, {
		name : "时间服务器告警",
		id : "13",
		src : "modules/timeAlarm/timeAlarm.html"
	}, {
		name : "被授时客户端告警",
		id : "14",
		src : "modules/clientAlarm/clientAlarm.html"
	}, {
		name : "websocket",
		id : "15",
		src : "modules/websocket/test.html"
	} ]
}, {
	name : "系统管理",
	id : "2",
	children : [ {
		name : "部门管理",
		id : "21",
		src : ""
	}, {
		name : "用户管理",
		id : "22",
		src : ""
	} ]
} ];

window.onload = function initProcessPage() {
	// 修改全局样式
	modifyCss();
	// 初始化菜单
	initHeaderMenu();
};

function modifyCss() {

	// 修改全局样式
	var framePageDivHeight = document.documentElement.clientHeight - 90;
	if (framePageDivHeight > 610) {
		document.getElementById("framePageDiv").style.height = document.documentElement.clientHeight
				- 90 + "px";
	} else {
		document.getElementById("framePageDiv").style.height = "610px";
	}
};

function initHeaderMenu() {

	// 初始化菜单
	var htmlMenu = "";

	for (var i = 0; i < headerMenuArray.length; i++) {

		// 顶层菜单
		htmlMenu += "<a";
		htmlMenu += " onclick='selectHeaderMenu(" + i + ")'";
		htmlMenu += " id='" + headerMenuArray[i].id + "'>";
		htmlMenu += "<span class='lBtn'>";
		htmlMenu += "<span class='lBtnText'>" + headerMenuArray[i].name
				+ "</span>";
		htmlMenu += "</span>";
		htmlMenu += "</a>";
	}

	document.getElementById("headerMenu").innerHTML = htmlMenu;

	// 初始化首页菜单
	selectHeaderMenu(0);
};

function selectHeaderMenu(i) {

	// 去掉原有的选中
	if (selectHeaderMenuId != "") {
		var selectHeaderMenuObj = document.getElementById(selectHeaderMenuId);
		if (selectHeaderMenuObj) {
			removeClass(selectHeaderMenuObj, "headerMenuActive");
		}
	}

	// 选中当前的标签
	selectHeaderMenuId = headerMenuArray[i].id;
	var currentHeaderMenuObj = document.getElementById(selectHeaderMenuId);
	if (currentHeaderMenuObj) {
		addClass(currentHeaderMenuObj, "headerMenuActive");
	}

	// 生成新二级菜单
	var htmlToolbar = "";
	for (var j = 0; j < headerMenuArray[i].children.length; j++) {

		htmlToolbar += "<a";
		htmlToolbar += " onclick='selectToolbar(\""
				+ headerMenuArray[i].children[j].id + "\",\""
				+ headerMenuArray[i].children[j].src + "\")'";
		htmlToolbar += " id='" + headerMenuArray[i].children[j].id + "'>";
		htmlToolbar += headerMenuArray[i].children[j].name;
		htmlToolbar += "</a>";
	}
	document.getElementById("manageToolbar").innerHTML = htmlToolbar;

	// 初始化二级菜单
	selectToolbar(headerMenuArray[i].children[0].id,
			headerMenuArray[i].children[0].src);
};

function selectToolbar(id, src) {

	// 去掉原有的选中
	if (selectToolbarId != "") {
		var selectToolbarObj = document.getElementById(selectToolbarId);
		if (selectToolbarObj) {
			removeClass(selectToolbarObj, "manageToolbarActive");
		}
	}

	// 选中当前的标签
	selectToolbarId = id;
	var currentToolbarObj = document.getElementById(selectToolbarId);
	if (currentToolbarObj) {
		addClass(currentToolbarObj, "manageToolbarActive");
	}

	// 跳转到当前页面
	document.getElementById("framePage").src = src;

};

function hasClass(obj, cls) {
	return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
};

function addClass(obj, cls) {
	if (!this.hasClass(obj, cls)) {
		obj.className += " " + cls;
	}
};

function removeClass(obj, cls) {
	if (hasClass(obj, cls)) {
		var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
		obj.className = obj.className.replace(reg, ' ');
	}
};